﻿Imports PTIC_GestionBDD
Imports Actuariat
Imports OMANE.Common.Model.Parametrage

Namespace ViewModels
    Public Class SegmentProvider

        Friend Shared model As ModelTauxGarantis = Locator.ModelStatic

        Public Shared Function GetAllVars() As IEnumerable
            If Not issetup Then Setup()
            If Not issetup Then Return Nothing

            Dim l As ObservableDictionary(Of String, Correspondance) = model.Segmentation.Retenue
            Dim q = From e In l
                    Select Name = e.Value.chOutil.Nom, Description = e.Value.description, Type = e.Value.chOutil.Format.typeAff.Name

            Return q.AsEnumerable
        End Function
        Public Shared Function GetAvailableVars(ByVal nomparam As String) As IEnumerable
            If Not issetup Then Setup()
            If Not issetup Then Return Nothing

            'Dim segRetenues As ObservableDictionary(Of String, Correspondance) = model.Univers.Segmentation.Retenue

            'Dim allsegs = model.Univers.ListeParametres(nomparam).ListeSegments()

            'If allsegs Is Nothing Then Return GetAllVars()

            'If allsegs.Count = 0 Then Return GetAllVars()
            'Enumerable.Except(segRetenues.Keys,all
            ''Dim func = Function(description As String, SegRetenues As IEnumerable(Of Correspondance))
            ''               Return From seg In SegRetenues Where seg.description = description Select seg
            ''           End Function
            'Dim cores = From seg In allsegs Select segRetenues(seg) 'func(e, l.Values).FirstOrDefault()

            ''If cores.Count = 1 Then Return GetAllVars()

            'Dim q = From e In segRetenues
            '        Where Not cores.Contains(e.Value)
            '        Select Name = e.Value.chOutil.Nom, Description = e.Value.description, Type = e.Value.chOutil.Format.typeAff.Name


            Dim segrestants = Enumerable.Except( _
                       model.Segmentation.Retenue.Keys, _
                       model.ListeParametres(nomparam).ListeSegments())



            Return From seg In segrestants
                     Select Name = model.Segmentation.Retenue(seg).chOutil.Nom,
                            Description = model.Segmentation.Retenue(seg).description,
                            Type = model.Segmentation.Retenue(seg).chOutil.Format.typeAff.Name
        End Function

        Public Shared Sub AddVar(ByVal nomparam As String, ByVal ParamArray varnames() As String)
            Dim param As IParametre = model.ListeParametres.Item(nomparam)
            Dim ItemAdded As Boolean = False
            For Each varname As String In varnames
                If Not param.ListeSegments.Contains(varname) Then
                    param.ListeSegments.Add(varname)
                    ItemAdded = True
                End If
            Next
            If ItemAdded Then param.MAJValeurs()
        End Sub
        Public Shared Sub RemoveVar(ByVal varname As String, ByVal nomparam As String)
            Dim param As IParametre = model.ListeParametres.Item(nomparam)
            Dim itemRemoved As Boolean = False
            If param.ListeSegments.Contains(varname) Then
                param.ListeSegments.Remove(varname)
                itemRemoved = True
            End If
            If itemRemoved Then param.MAJValeurs()
        End Sub

        Private Shared issetup As Boolean = False
        Private Shared Sub Setup()
            If model Is Nothing Then Return
            If model.Segmentation Is Nothing Then Return
            AddHandler model.Segmentation.SegmentationRetenueChanged, AddressOf modelUniversSegmentationSegmentationRetenueChanged
            issetup = True
        End Sub

        Private Shared Sub modelUniversSegmentationSegmentationRetenueChanged(ByVal sender As Object, ByVal e As EventArgs)
            RaiseEvent SegmentationChanged(sender, EventArgs.Empty)
        End Sub
        Friend Shared Event SegmentationChanged As EventHandler

    End Class
End Namespace